<script language="javascript">
$(function(){  
	$('#classID').change(function(){
		getGoodsInfo();
	});

	$("#customID").change(function(){
		getClassSelectVal();
	});

	$('.orderTr').each(function(){
		var tr2 = $(this);
		$(this).bind("click",function()   
		{
			var tr = $(this);			

			$('.orderTr').each(function(){
				var tr1 = $(this);
				tr1.css("background-color", "#FFFFFF");
			})

			currentRowIndex = tr.find("td").get(0).innerHTML - 1;
			tr.css("background-color", "#4bf91c");
		});		
		currentRowIndex = tr2.find("td").get(0).innerHTML - 1;

		bindEvent();
	});

	var gRowNum = 1;
    $("#addRow").click(function() {		
　　　　//复制一行
         var tr = $("#OrderList>tbody>tr:first").clone();		 
		 var index = gRowNum;
		 var startClassValue = 1;
		 var rowAddLoop = 1;
		 var classInterval = 1;
		 var i = 0;
		 var classStr;
		 var classArr;
		 var strSplitLen;
		 var classStrPre;
		 var classStrBehind;

		 if ($("#classNum").val() != "")
		 {
			 if (tr.find("td").eq(25).children('input').attr("value") == "")
			 {
				alert("请填写第一行的班级！");
			 }
			else
			{
				classStr = tr.find("td").eq(25).children('input').attr("value");
				classArr = classStr.match(/\d+/g);
				startClassValue = classArr[classArr.length - 1];
				strSplitLen = classStr.lastIndexOf(startClassValue);
				classStrPre = classStr.substr(0, strSplitLen);
				classStrBehind = classStr.substr(strSplitLen + startClassValue.length);
				rowAddLoop = $("#classNum").val();
				startClassValue = parseInt(startClassValue);
			}
		 }		 

		 if ($("#classInterval").val() != "")
		 {
			classInterval = parseInt($("#classInterval").val());
		 }
		 for ( i = 0; i < rowAddLoop; i++)
		 {
			tr = $("#OrderList>tbody>tr:first").clone()
			startClassValue += classInterval;
			index = gRowNum; 
			tr.find("td").get(0).innerHTML = ++gRowNum;
			tr.find("td").eq(1).children('input').attr("checked", false);		 		 
			tr.find("td").eq(1).children('input').attr("name", "isSingleArr["+index+"]");
			tr.find("td").eq(2).children('select').attr("value", $("#OrderList>tbody>tr:first").find("td").eq(2).children('select').attr("value"));
			tr.find("td").eq(3).children('input').attr("value", "");

			tr.find("td").eq(16).children('select').attr("value", $("#OrderList>tbody>tr:first").find("td").eq(16).children('select').attr("value"));

			tr.find("td").eq(18).children('input').attr("name", "isWeakCityArr["+index+"]");

			tr.find("td").eq(19).children('select').attr("value", $("#OrderList>tbody>tr:first").find("td").eq(19).children('select').attr("value"));

			tr.find("td").eq(20).children('select').attr("value", $("#OrderList>tbody>tr:first").find("td").eq(20).children('select').attr("value"));

			tr.find("td").eq(21).children('select').attr("value", $("#OrderList>tbody>tr:first").find("td").eq(21).children('select').attr("value"));

			tr.find("td").eq(23).children('select').attr("value", $("#OrderList>tbody>tr:first").find("td").eq(23).children('select').attr("value"));

			tr.find("td").eq(24).children('input').attr("name", "isSchoolArr["+index+"]");
			if ($("#classNum").val() != "")
			{
				tr.find("td").eq(25).children('input').attr("value", classStrPre + startClassValue + classStrBehind);
			}
			tr.show();
			tr.appendTo("#OrderList>tbody");
		 }

		 $('.orderTr').each(function(){
			var tr2 = $(this);
			$(this).bind("click",function()   
			{
				var tr = $(this);
				$('.orderTr').each(function(){
					var tr1 = $(this);
					tr1.css("background-color", "#FFFFFF");
				})
				
				currentRowIndex = tr.find("td").get(0).innerHTML - 1;
				tr.css("background-color", "#4bf91c");
			});	
			
			currentRowIndex = tr2.find("td").get(0).innerHTML - 1;
			bindEvent();		/* 增加一行后重新绑定相关事件 */
		 });		 
      });

	  $("#removeRow").click(function() {
		var i = 1;
		if (currentRowIndex == 0)
		{
			return 0;
		}
		$("#OrderList>tbody>tr").eq(currentRowIndex).remove();
		gRowNum = gRowNum - 1;
		$('.orderTr').each(function(){
			var tr3 = $(this);
			tr3.find("td").get(0).innerHTML = i;
			i++;
		 });

		bindEvent();		/* 删除一行后重新绑定相关事件 */
      });
		
	  bindEvent();			/* 第一次要绑定相关事件 */
		
	$(".customClear").click(function()
	{ 
		$(this).parent(".unitBox").children("input").each(
			function()
			{
				$(this).attr("value", "");
				$(this).change();				/* 触发change事件 */
			}
		);		
	}); 

	/* 内容保存 */
	$("#contentSave").click(function()
	{
		$("#isChecked").attr("value",'2');
		$("#orderManageAddForm").submit();
	});
});

/* 当前修改的table的编号 全局变量
 * 点击某一列时，currentRowIndex为该列的编号
 * 新添加一列时，currentRowIndex为新添加的列的编号
 * 当前的操作都是基于currentRow
 */
var currentRowIndex = 0;

function getTermSelectVal()
{ 
	/* 取消单期 */
	return ;
	if ($("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(1).children('input').attr("checked") == false)
	{
		return ;
	}

	$.getJSON(
		"__URL__/getSelect/type/1/postCode/"+$("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(2).children('select').val()+"/year/"+$("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(4).children('input').val()+"/month/"+$("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(5).children('input').val(),
		function(json)
		{         
			var termID = $("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(9).children('.termID');         
			$("option",termID).remove();   
			$.each(json,function(index,array)
			{             
				var option = "<option value='"+array['id']+"'>"+array['title']+"</option>";
				termID.append(option);         
			});     
		}
	); 
}


/**
 * 第一次初始化
 * 增加新列
 * 切换不同的列时，都需要调用绑定事件的操作 
 */
function bindEvent()
{

	$("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(2).children('select').change(function(){
		getTermSelectVal();
	});
	
	$("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(4).children('input').change(function(){
		getTermSelectVal();
	});

	$("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(5).children('input').change(function(){
		getTermSelectVal();
	});

	$("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(24).children('input').change(function(){
		getSchoolSelectVal();
	});
	

	$("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(19).children('select').change(function(){
		getSendGoodsSelectVal();
	});
}

function getClientSelectVal()
{ 
	var isOldClient;

	if ($("#isOldClient").attr("checked") == true)
	{
		isOldClient = 1;
	}
	else
	{
		isOldClient = 0;
	}

	$.getJSON(
		"__URL__/getSelect/type/2/isOldClient/"+isOldClient,
		function(json)
		{         
			var customID = $("#customID");         
			$("option",customID).remove();   
			$.each(json,function(index,array)
			{             
				var option = "<option value='"+array['id']+"'>"+array['title']+"</option>";
				customID.append(option);         
			});     
		}
	); 
}

function getSchoolSelectVal()
{
	var isSchoolSend;

	var isSchool = $("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(24).children('.isSchool');

	if (isSchool.attr("checked") == true)
	{
		isSchoolSend = 1;
	}
	else
	{
		isSchoolSend = 0;
	}

	$.getJSON(
		"__URL__/getSelect/type/3/isSchool/"+isSchoolSend,
		function(json)
		{         
			var schoolID = $("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(23).children('.schoolID');      
			$("option",schoolID).remove();   
			$.each(json,function(index,array)
			{             
				var option = "<option value='"+array['id']+"'>"+array['title']+"</option>";
				schoolID.append(option);         
			});     
		}
	); 
}

function getClassSelectVal()
{
	$.getJSON(
		"__URL__/getSelect/type/4/customId/"+$("#customID").val(),
		function(json)
		{         
			var classID = $("#classID");         
			$("option",classID).remove();   
			$.each(json,function(index,array)
			{             
				var option = "<option value='"+array['id']+"'>"+array['title']+"</option>";
				classID.append(option);         
			});     
		}
	);
}

function getGoodsInfo()
{
    if ($("#classID").attr("value"))
	{
		$.getJSON(
		"__URL__/getSelect/type/5/goodsId/"+$("#classID").val(),
		function(json)
		{
			$("input[name='recPeopleArr[]']")[currentRowIndex].value = json['recName'];
			$("input[name='recTelArr[]']")[currentRowIndex].value = json['tel'];
			$("input[name='recAddressArr[]']")[currentRowIndex].value = json['address'];
			if (json['isSchool'])
			{
				$("input[name='isSchoolArr["+currentRowIndex+"]']").attr("checked", true);
			}
			
			$("input[name='classArr[]']")[currentRowIndex].value = json['className'];
			$("input[name='cityNameArr[]']")[currentRowIndex].value = json['cityName'];
			$("select[name='sendGoodsSortIDArr[]']")[currentRowIndex].value = json['sendSortID'];
			$("select[name='sendGoodsTypeIDArr[]']")[currentRowIndex].value = json['sendTypeID'];
			$("select[name='provinceIDArr[]']")[currentRowIndex].value = json['provinceID'];
			$("select[name='schoolIDArr[]']")[currentRowIndex].value = json['schoolID'];
			$("input[name='memoArr[]']")[currentRowIndex].value = json['memo'];
			$("input[name='recTelphoneArr[]']")[currentRowIndex].value = json['phone'];
			$("input[name='recSpareTelArr[]']")[currentRowIndex].value = json['spareTel'];
			$("input[name='recFaxArr[]']")[currentRowIndex].value = json['fax'];
			$("input[name='packetTypeArr[]']")[currentRowIndex].value = json['packetType'];
			$("input[name='zipCodeArr[]']")[currentRowIndex].value = json['zipCode'];
			$("select[name='sendCyleIDArr[]']")[currentRowIndex].value = json['sendOrderCyleID'];
			
			$("input[name='payPerson']").attr("value", json['payName']);
		});
	}
}


function getSendGoodsSelectVal()
{
	var sendSortID = $("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(19).children('select').attr("value");
	$.getJSON(
		"__URL__/getSelect/type/6/sendSortID/"+sendSortID,
		function(json)
		{         
			var sendGoodsType = $("#OrderList>tbody>tr").eq(currentRowIndex).find('td').eq(20).children('select');         
			$("option",sendGoodsType).remove();   
			$.each(json,function(index,array)
			{             
				var option = "<option value='"+array['id']+"'>"+array['title']+"</option>";
				sendGoodsType.append(option);         
			});     
		}
	);
}
</script>

<div class="page">
	<div class="pageContent">
<style type="text/css">
.BlockHeader
{
	text-align:left;
	font-size:14;
	font-weight:bold; 
	height:15px
}
.pageFormContent label {
float: left;
width: 80px;
padding: 0 5px;
line-height: 21px;
}
</style>
	
	<form method="post" id="orderManageAddForm" action="__URL__/insert/navTabId/{$Think.const.MODULE_NAME}" class="pageForm required-validate" onsubmit="return validateCallback(this, dialogAjaxDone)">
		<input type="hidden" name="insertPerson" value="{$insertPerson}"/> 
		<input type="hidden" name="isChecked" id="isChecked" value="0"/> 
		<div class="pageFormContent" layoutH="56">
			<span  class="BlockHeader">状态信息</span>
			<div class="divider"></div>
				<label>状态：</label>
				<input name="orderStatus" type="text" size="18" value="制作中" readonly="readonly"/>
				<label>业务经理：</label>
				<select name="employeeID" readonly="readonly">
					<option selected value="{$EmpoyeeId}">{$EmployeeName}</option>
				</select>
			<div class="divider"></div>
				<span  class="BlockHeader">客户信息</span>
			<div class="divider"></div>
				<span class="unitBox" style="float:left;">
					<label>客户名称：</label>
					<input name="customID" value="" type="hidden" id="customID">
					<input name="customName" value="" size="18" type="text" class="required"/>
					<a class="btnLook" href="__APP__/CustomSelect/index" lookupGroup="" lookupName="customID">查找</a>&nbsp;&nbsp;
					<a class="customClear" href="#">清空</a>
				</span>
				<span style="float:left;">
					<label>收货信息：</label>
					<select id="classID">
						<option value="">--请选择--</option>
					<volist name="classList" id="classVo">
						<option value="{$classVo['id']}">{$classVo['name']}</option>
					</volist>
					</select>
				</span>
			<div class="divider"></div>
			<span  class="BlockHeader">订单信息</span>
			<div class="divider"></div>
				<label>签单日期：</label>
				<input type="text" name="orderTime" value="{$orderTime}" class="date" size="18" /><a class="inputDateButton" href="javascript:;">选择</a>

				<label>订单编号：</label>
				<input name="contractID" type="text" class="required" value="{$contractIdCode}" size="18" readonly="readonly"/>

				<label>订单来源：</label>
				<select name="magazineOriginNameID" >
					<option value="">--请选择--</option>
				<volist name="MagazineOriginList" id="magazineOriginVo">
					<option <eq name="magazineOriginVo.name" value="绿风公司" >selected</eq> value="{$magazineOriginVo['id']}">{$magazineOriginVo['name']}</option>
				</volist>
				</select>
				
				<label>订单批次：</label>
				<input name="batch" type="text" value="{$batch}" size="18"/>
				<br/>
				<br/>

				<label>付款日期：</label>
				<input type="text" name="payTime" class="date" size="18" /><a class="inputDateButton" href="javascript:;">选择</a>

				<label>付款人：</label>
				<input name="payPerson" type="text" size="18" class="required"/>

				<label>付款方式：</label>
				<select name="payType" >
					<option value="">--请选择--</option>
				<volist name="PayTypeList" id="PayTypeVo">
					<option value="{$PayTypeVo['id']}">{$PayTypeVo['name']}</option>
				</volist>
				</select>
				<span style="display:inline;">
					<input type="checkbox" name="isPay" value="1"/>
					是否付款
				</span>				
			<div class="divider"></div>

			<table width="100%" id="OrderList">
				<thead>
					<tr height="40">
						<th width="60">编号</th>			<!-- 0 -->
						<th width="80" style="display:none;">单期</th>			<!-- 1 -->
						<th width="80">报刊名称</th>		<!-- 2 -->
						<th width="60">份数</th>			<!-- 3 -->
						<th width="60">年度</th>			<!-- 4 -->						
						<th width="60">起月<!--(单期月)--></th>			<!-- 5 -->
						<th width="60">止月</th>			<!-- 6 -->
						<th width="60">磁带数</th>		<!-- 7 -->
						<th width="60">答案数</th>		<!-- 8 -->
						<th width="80" style="display:none;">期数</th>			<!-- 9 -->						
						<th width="80">收货人</th>		<!-- 10 -->
						<th width="80">电话</th>			<!-- 11 -->
						<th width="80">手机</th>			<!-- 12 -->
						<th width="80">备用电话</th>			<!-- 13 -->
						<th width="80">传真</th>			<!-- 14 -->
						<th width="80" style="display:none;">包装方式</th>		<!-- 15 -->
						<th width="80">省份</th>			<!-- 16 -->
						<th width="80">城市</th>			<!-- 17 -->
						<th width="80">是否薄弱县</th>	<!-- 18 -->
						<th width="80">发货类型</th>		<!-- 19 -->
						<th width="80">发货方式</th>		<!-- 20 -->
						<th width="80">发货周期</th>		<!-- 21 -->
						<th width="80">收货地址</th>		<!-- 22 -->
						<th width="100">单位</th>		<!-- 23 -->
						<th width="80">是否学校</th>		<!-- 24 -->
						<th width="80">班级</th>			<!-- 25 -->
						<th width="80">邮编</th>			<!-- 26 -->
						<th width="100">备注</th>		<!-- 27 -->
					</tr>
				<thead>
				<tbody>
					<tr class="orderTr">
						<td>
							1
						</td>
						<td style="display:none;"><input type="checkbox" name="isSingleArr[0]" value="1" class="isSingle"/></td>
						<td>
							<select name="postCodeArr[]" class="required">
								<option value="">--请选择--</option>
							<volist name="MagazineList" id="MagazineVo">
								<option value="{$MagazineVo['postCode']}">{$MagazineVo['name']}</option>
							</volist><font color="red">*</font>
							</select>
						</td>
						<td><input name="orderNumArr[]" type="text" size="10" class="required" /></td>
						<td><input name="orderYearArr[]" type="text" value="{$year}" size="10" class="digits"/></td>					
						<td><input name="beginOrderDateArr[]" type="text" size="10" class="required" class="digits"/></td>
						<td><input name="endOrderDateArr[]" type="text" size="10" class="required" class="digits"/></td>
						<td>
							<input name="tapeNumArr[]" type="text" size="10"/>
						</td>
						<td>
							<input name="answerNumArr[]" type="text" size="10"/>
						</td>
						<td style="display:none;">
							<select name="termIDArr[]" class="termID">
								<option value="">--请选择--</option>
							<volist name="MagazineTerrmList" id="MagazineTerrmVo">
								<option value="{$MagazineTerrmVo['id']}">{$MagazineTerrmVo['name']}</option>
							</volist>
							</select>
						</td>
						<td><input name="recPeopleArr[]" type="text" size="15"/></td>
						<td><input name="recTelArr[]" type="text" size="15"/></td>
						<td><input name="recTelphoneArr[]" type="text" size="15"/></td>
						<td><input name="recSpareTelArr[]" type="text" size="15"/></td>
						<td><input name="recFaxArr[]" type="text" size="15"/></td>
						<td style="display:none;"><input name="packetTypeArr[]" type="text" size="15"/></td>
						<td>
							<select name="provinceIDArr[]">
								<option value="">--请选择--</option>
							<volist name="ProvinceList" id="ProvinceVo">
								<option value="{$ProvinceVo['id']}">{$ProvinceVo['name']}</option>
							</volist>
							</select>
						</td>
						<td>
							<input name="cityNameArr[]" type="text" size="15"  value=""/>
						</td>
						<td>	
							<input type="checkbox" name="isWeakCityArr[0]" value="1"/>
						</td>
						<td>
							<select name="sendGoodsSortIDArr[]" class="sendGoodsSort">
								<option value="">--请选择--</option>
							<volist name="SendGoodsSortList" id="SendGoodsSortVo">
								<option value="{$SendGoodsSortVo['id']}">{$SendGoodsSortVo['name']}</option>
							</volist>
							</select>
						</td>
						<td>
							<select name="sendGoodsTypeIDArr[]">
								<option value="">--请选择--</option>
							<volist name="SendGoodsTypeList" id="SendGoodsTypeVo">
								<option value="{$SendGoodsTypeVo['id']}">{$SendGoodsTypeVo['name']}</option>
							</volist>
							</select>
						</td>
						<td>
							<select name="sendCyleIDArr[]">
								<option value="">--请选择--</option>
							<volist name="SendOrderCyleList" id="SendOrderCyleVo">
								<option value="{$SendOrderCyleVo['id']}">{$SendOrderCyleVo['name']}</option>
							</volist>
							</select>
						</td>
						<td>
							<input name="recAddressArr[]" type="text" size="15"/>
						</td>
						<td>
							<select name="schoolIDArr[]" class="schoolID" width="50">
								<option value="">--请选择--</option>
							<volist name="SchoolList" id="SchoolVo">
								<option value="{$SchoolVo['id']}">{$SchoolVo['name']}</option>
							</volist>
							</select>
						</td>							
						<td>	
							<input type="checkbox" name="isSchoolArr[0]" value="1" class="isSchool"/>
						</td>
						<td>
							<input name="classArr[]" type="text" size="15"/>
						</td>
						<td>
							<input name="zipCodeArr[]" type="text" size="15"/>
						</td>						
						<td>
							<input name="memoArr[]" type="text" size="15"  value=""/>
						</td>
					</tr>
					
				</tbody>
			</table>			
		</div>
		<div class="formBar">
			<ul>
				<li>
					<label>班级个数：</label>
					<input type="text" size="10" id="classNum"/> 
					<label>班级间隔：</label>
					<input type="text" size="10" id="classInterval"/> 
				</li>
				<li><div class="buttonActive"><div class="buttonContent"><button id="removeRow" type="button">删除行</button></div></div></li>
				<li><div class="buttonActive"><div class="buttonContent"><button id="addRow" type="button">添加新行</button></div></div></li>
				<li><div class="buttonActive"><div class="buttonContent"><button id="contentSave" type="button">保存</button></div></div></li>
				<li><div class="buttonActive"><div class="buttonContent"><button type="submit">提交</button></div></div></li>
				<li><div class="button"><div class="buttonContent"><button type="button" id="close" class="close">取消</button></div></div></li>
			</ul>
		</div>
	</form>
	
	</div>
</div>


